<template>
  <div class="organ">
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      size="small"
    >
      <el-form-item label="机构名称">
        <el-input
          v-model="formInline.organName"
          placeholder="机构名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="机构类型">
        <el-select v-model="formInline.organTypeId" placeholder="请选择" clearable>
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="add('add')">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" border style="width: 100%" height="620px">
      <el-table-column prop="organName" label="机构名称"> </el-table-column>
      <el-table-column prop="description" label="机构简介"> </el-table-column>
      <el-table-column prop="contactNo" label="联系方式"> </el-table-column>
      <el-table-column prop="address" label="剧本馆地址"> </el-table-column>
      <el-table-column prop="organAvatar" label="机构图片">
        <template slot-scope="scope">
          <el-image
            :src="userAvatar(scope.row.organAvatar)"
            :preview-src-list="[userAvatar(scope.row.organAvatar)]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="人均费用"> </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="add('edit', scope.row)" size="mini"
            >编 辑</el-button
          >
          <el-button type="danger" @click="del(scope.row.id, false)" size="mini"
            >删 除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block pageCont">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSizes"
        layout="total, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="type == 'add' ? '新增' : '编辑'"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form
        :model="form"
        ref="form"
        label-width="100px"
        class="demo-ruleForm"
        size="small"
      >
        <el-form-item label="机构类型">
          <el-select v-model="form.organTypeId" placeholder="请选择" clearable>
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="机构名称">
          <el-input v-model="form.organName" placeholder="机构名称"></el-input>
        </el-form-item>
        <el-form-item label="机构简介">
          <el-input
            v-model="form.description"
            placeholder="机构简介"
          ></el-input>
        </el-form-item>
        <el-form-item label="机构图">
          <el-upload
            class="avatar-uploader"
            :show-file-list="false"
            :http-request="upload"
            action="#"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="form.contactNo" placeholder="联系方式"></el-input>
        </el-form-item>
        <el-form-item label="剧本馆地址">
          <el-input v-model="form.address" placeholder="剧本馆地址"></el-input>
        </el-form-item>
        <el-form-item label="人均费用">
          <el-input v-model="form.price" placeholder="人均费用"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm()" size="small">取 消</el-button>
        <el-button type="primary" @click="submit()" size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { OrganList, OrganDel, OrganEdit } from "@/api/organ";
import { UpLoadImg } from "@/api/common";
import baseConfig from "@/utils/config";
export default {
  data() {
    return {
      tableData: [],
      formInline: {},
      currentPage: 1,
      total: 0,
      pageSizes: 20,
      dialogVisible: false,
      form: {},
      type: "add",
      typeList: [
        { name: "短视频", id: "1" },
        { name: "剧本杀", id: "2" },
        { name: "歌词", id: "3" },
      ],
      imageUrl: "",
    };
  },
  created() {
    this.getList();
  },
  watch: {
    dialogVisible(nv) {
      if (!nv) this.resetForm();
    },
  },
  methods: {
    userAvatar(url) {
      return baseConfig.dev + url;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },
    getList() {
      OrganList({ page: this.currentPage, ...this.formInline }).then((res) => {
        this.tableData = res.data.list;
        this.total = res.data.total;
      });
    },
    del(id) {
      OrganDel({ id: id }).then((res) => {
        if (res.code == 0) {
          this.$message.success("删除成功");
          this.getList();
        } else {
          this.$message.error(res.data);
        }
      });
    },
    upload(params) {
      let file = params.file;
      // 根据后台需求数据格式
      let form = new FormData();
      // 文件对象
      form.append("files", file);
      UpLoadImg(params).then((res) => {
        if (res.code == 0) {
          this.form.organAvatar = res.data[0];
          this.imageUrl = baseConfig.dev + "" + res.data[0];
        } else {
          this.$message.error(res.data);
        }
      });
    },
    add(type, row) {
      this.type = type;
      this.dialogVisible = true;
      if (row) {
        this.form = JSON.parse(JSON.stringify(row));
        this.imageUrl = baseConfig.dev + "" + this.form.organAvatar;
      }
    },
    resetForm() {
      this.dialogVisible = false;
      this.imageUrl = "";
      this.form = {};
      this.$refs["form"].resetFields();
    },
    submit() {
      OrganEdit(this.form).then((res) => {
        if (res.code == 0) {
          this.$message.success(this.type == "add" ? "新增成功" : "编辑成功");
          this.getList();
          this.resetForm();
        } else {
          this.$message.error(res.data);
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.organ {
  ::v-deep {
    .el-image {
      width: 50px !important;
      height: 50px !important;
      .el-image__error {
        font-size: 12px;
      }
    }
    .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 80px;
      height: 80px;
      line-height: 80px;
      text-align: center;
    }
    .avatar {
      width: 80px;
      height: 80px;
      display: block;
    }
  }
}
</style>